# -*- coding:utf-8 -*-
from flask import request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao_v2.userOauthDao import UserOauthDao
import time
from . import app

localTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))

CORS(app, supports_credentials=True)


@app.route('/queryUserOauthById', methods=['POST'])
def queryUserOauthByIdNoToken():
    if not request.json:
        abort(400)
    id = request.json.get("id")
    try:
        total_count, userOauthJSON = UserOauthDao().queryUserOauthById(id)
        if total_count:
            return jsonify({"message": {"code": 1, "status": "queryUserOauth OK"}, "info": userOauthJSON})
        else:
            return jsonify({"info": [], "message": {"code": 0, "status": "No data"}})

    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})

@app.route('/queryUserOauthByCondition', methods=['POST'])
def queryUserOauthByConditionNoToken():
    if not request.json:
        abort(400)
    userId = request.json.get("userId")
    pageIndex = request.json.get("pageIndex", 1)  # 传递页面索引号，默认为1

    pageSize = request.json.get("pageSize", 10)
    try:
        total_count, rs = UserOauthDao().queryUserOauthByCondition(userId=userId, pageIndex=pageIndex,pageSize=pageSize)
        if total_count:
            return jsonify({"message": {"code": 1, "status": "queryUserOauth OK"}, "totalCount": total_count, "info": rs})
        else:
            return jsonify({"info": [], "message": {"code": 0, "status": "No data"}})

    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})

@app.route('/insertUserOauth', methods=['POST'])
def insertUserOauthNoToken():
    if not request.json:
        abort(400)

    Info = {
        'userId': request.json.get('userId'),  # 必填，请传递userd
        'userName': request.json.get('userName'),  #
        'oauthName': request.json.get('oauthName'),  #
        'oauthAccessToken': request.json.get('oauthAccessToken'),  #
        'oauthOpenId': request.json.get('oauthOpenId')  #
    }
    if not Info:
        return jsonify({"message": {"code": 0, "info": "信息填写未完整"}})
    try:
        rs = UserOauthDao().addUserOauth(Info)
        # return jsonify({"message": {"code": 0, "info": "信息填写未完整"}})
        if rs:
            return jsonify(
                {"massage": {"code": 1, "info": "addUserOauth OK"}})  # 添加函数调用成功，返回添加的信息（不包括userRecharge的Id）
        else:
            return jsonify({"message": {"code": 0, "info": "addUserOauth Error"}})  # 添加函数调用失败
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})


@app.route('/updateUserOauth', methods=['POST'])
def updateUserOauthNoToken():
    try:
        id = request.json['id']
    except:
        return jsonify({"message": {"code": 0, "status": "请传入 id"}})
    oldInfo = UserOauthDao().selectOauth(id)

    Info = {
        'id': request.json['id'],
        'userId': request.json.get('userId', oldInfo[1]),  # 必填，请传递userd
        'userName': request.json.get('userName', oldInfo[2]),  #
        'oauthName': request.json.get('oauthName', oldInfo[3]),  #
        'oauthAccessToken': request.json.get('oauthAccessToken', oldInfo[4]),  #
        'oauthOpenId': request.json.get('oauthOpenId', oldInfo[5])  #
    }
    if not Info:
        return jsonify({"message": {"code": 0, "info": "信息填写未完整"}})

    rs = UserOauthDao().updateUserOauth(Info)
    # return jsonify({"message": {"code": 0, "info": "信息填写错误"}})
    if rs:
        return jsonify(
            {"info": Info, "massage": {"code": 1, "info": "updateUserOauth OK"}})  # 添加函数调用成功，返回添加的信息
    else:
        return jsonify({"message": {"code": 0, "info": "updateUserOauth Error"}})  # 添加函数调用失败


@app.route('/deleteUserOauth', methods=['POST'])  # 根据id数组删除zzh_user_recharge表指定记录,传入格式为 "ids":[1,2,3,4]
def deleteUserOauthNoToken():
    ids = request.json.get("ids")
    if not ids:
        return jsonify({"message": "请输入待删除的userRecharge id数组！"})
    rs = UserOauthDao().deleteUserOauth(ids)
    if rs:
        return jsonify({"message": {"code": 1, "info": "DeleteUserOauth OK"}})
    else:
        return jsonify({"message": {"code": 0, "info": "DeleteUserOauth Error"}})
